<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Company;

/**
 * CompanySearch represents the model behind the search form about `common\models\Company`.
 */
class CompanySearch extends Company
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'is_display', 'created_at', 'updated_at'], 'integer'],
            [['company_name', 'url'], 'safe'],
            [['sort'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Company::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'sort' => $this->sort,
            'is_display' => $this->is_display,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'company_name', $this->company_name])
            ->andFilterWhere(['like', 'url', $this->url]);
        $dataProvider->sort->defaultOrder = ['id' => SORT_DESC];

        return $dataProvider;
    }
    
    /**
     *  获取所有公司
     */
    public static function getAllCompany($company_name,$type='search')
    {
        $array = array();
        if($company_name){
            $list = Company::find()->where(['company_name'=>$company_name,'is_display'=>'1'])->orderBy("sort asc")->asArray()->all();
        }else{
            $list = Company::find()->where(['is_display'=>'1'])->orderBy("sort asc")->asArray()->all();
        }
        if($type=='add'){
            $array[''] = '--请选择--';
        }else{
            $array[''] = '所有公司';
            
        }
        
        foreach ($list as $k => $v ){
            $array[$v['id']]=$v['company_name'];
        }
//        / print_r($array);
        
        //print($array);
        return $array;
        
    }
}
